// search.js
const regeneratorRuntime = require('../../lib/runtime');
const wx = require('../../lib/wx');
const Api = require('../../utils/api.js');
const wxapp = require('../../lib/wxapp');

const searchHistoryKey = 'searchHistoryKey';

// 获取应用实例
const app = getApp();
Page({
  data: {
    searchFocus: true,
    searchValue: '',
    goSearch: false,
    historySearchList: [
      {
        name: 'HABA',
        url: '../SearchList/SearchList',
      },
      {
        name: 'Gucci',
        url: '../SearchList/SearchList',
      },
      {
        name: 'Johnstons of Elgin',
        url: '../SearchList/SearchList',
      },
    ],
    hotSearchList: [
      {
        name: 'Gucci',
        url: '../SearchList/SearchList',
      },
      {
        name: '羽绒服',
        url: '../SearchList/SearchList',
      },
      {
        name: '毛衣',
        url: '../SearchList/SearchList',
      },
      {
        name: 'Acne Studios',
        url: '../SearchList/SearchList',
      },
      {
        name: 'Johnstons of Elgin',
        url: '../SearchList/SearchList',
      },
      {
        name: 'Jimmy Choo',
        url: '../SearchList/SearchList',
      },
      {
        name: '大衣',
        url: '../SearchList/SearchList',
      },
    ],
    filterActive: '综合',
    sortStatus: '',
    brandDetail: {
      logo: 'http://www.haba.com.cn/images/logo3.png',
      name: 'HABA',
      description: 'HABA是“HealthAidBeautyAid”的缩写，意为健康与美丽的好帮手。HABA以“追求真正的美丽”，唤醒肌肤与生俱来自然治愈力，只给肌肤补充绝不超过最低限度的所需，绝对不添加刺激或损害肌肤的成分为经营理念。',
      list:[
        {
          imgUrl: '../../images/products/product_img1.png',
          url: '../productDetail/productDetail',
          brandName: 'HABA',
          description: '日本无添加主义 丝滑泡沫洁面皂 80g',
          current_price: '189.00',
          orign_price: '315.00',
          shop: 'HABA旗舰店',
          people: 0,
          joined: 0,
        },
        {
          imgUrl: '../../images/products/product_img2.png',
          url: '../productDetail/productDetail',
          brandName: 'HABA',
          description: '日本无添加主义 丝滑泡沫洁面皂 80g',
          current_price: '136.00',
          orign_price: '226.00',
          shop: 'HABA旗舰店',
          people: 2,
          joined: 2,
        },
        {
          imgUrl: '../../images/products/product_img1.png',
          url: '../productDetail/productDetail',
          brandName: 'HABA',
          description: '日本无添加主义 丝滑泡沫洁面皂 80g',
          current_price: '129.00',
          orign_price: '215.00',
          shop: 'HABA旗舰店',
          people: 0,
          joined: 0,
        },
        {
          imgUrl: '../../images/products/product_img2.png',
          url: '../productDetail/productDetail',
          brandName: 'HABA',
          description: '日本无添加主义 丝滑泡沫洁面皂 80g',
          current_price: '216.00',
          orign_price: '360.00',
          shop: 'HABA旗舰店',
          people: 0,
          joined: 0,
        },
        {
          imgUrl: '../../images/products/product_img1.png',
          url: '../productDetail/productDetail',
          brandName: 'HABA',
          description: '日本无添加主义 丝滑泡沫洁面皂 80g',
          current_price: '53.00',
          orign_price: '89.00',
          shop: 'HABA旗舰店',
          people: 2,
          joined: 0,
        },
        {
          imgUrl: '../../images/products/product_img2.png',
          url: '../productDetail/productDetail',
          brandName: 'HABA',
          description: '日本无添加主义 丝滑泡沫洁面皂 80g',
          current_price: '213.00',
          orign_price: '355.00',
          shop: 'HABA旗舰店',
          people: 0,
          joined: 0,
        },
      ],
    },
    actionActive: false,
    animationData: {},
    windowHeight: 0,
    filterDetail: {
      isOnly: false,
      type: [
        {
          title: '款式',
          isMultiple: true,
          value: ['男款', '女款', '儿童',],
        },
        {
          title: '分类',
          isMultiple: true,
          value: ['包包', '服装', '鞋履', '配饰', '家居',],
        },
        {
          title: '价格区间',
          isMultiple: false,
          value: ['500元以下', '500元 - 1000元', '1000元 - 2000元', '2000元 - 5000元', '5000元以上',],
        }
        //,
        //{
        //  title: '来源',
        //  isMultiple: true,
        //  value: ['BNKR', 'East Dane', 'Farfetch', 'FeelUnique', 'FWRD', 'Harrods', 'Kll ArtStore'],
        //},
      ],
    },
  },

  goBackHome: wxapp.handler(function* goBackHome(next){
    wx.navigateBack({
      delta: 1,
    });

    yield next;
  }),

  goToMenu: wxapp.handler(function* goToMenu(next, e){
    const url = e.currentTarget.dataset.url;
    wx.reLaunch({
      url,
    });

    yield next;
  }),

  removeHistory: wxapp.handler(function* removeHistory(next){
    this.setData({
      historySearchList: [],
    });

    wx.setStorageSync(searchHistoryKey, []); 

    yield next;
  }),

  changeSearchInput: wxapp.handler(function* changeSearchInput(next, e){
    if(e.detail.value === ''){
      this.setData({
        goSearch: false,
      });
    }

    yield next;
  }),

  goToSearchList: wxapp.handler(function* goToSearchList(next, e){
    const searchValue = e.target.dataset.value;
    this.setData({
      searchValue:searchValue
    });

    var that = this;
    var getGoodsList = Api.getGoodsList();
    // 商品列表
    wx.request({
      url: getGoodsList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        title: searchValue
      },
      complete: function (res) {
        that.setData({
          searchValue,
          brandDetail: res.data.data,
          goSearch: true,
        });
      }
    });



    yield next;
  }),
  goodsSearch: wxapp.handler(function* goodsSearch(next, e) {
    let activeType = this.data.activeType;
    var that = this;
    var getGoodsList = Api.getGoodsList();
    //var class_id = this.data.class_id;

    this.animation.right('-642rpx').step();

    this.setData({
      animationData: this.animation.export(),
      actionActive: false,
    });
    console.log(1);
    // 商品列表
    wx.request({
      url: getGoodsList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        //class_sub_id: class_id,
        activeType: activeType,
        title: that.data.searchValue
      },
      complete: function (res) {
        that.setData({ brandDetail: res.data.data });
      }
    });
    console.log(2);


    yield next;
  }),
  goToDetail: wxapp.handler(function* goToDetail(next, e){
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });

    yield next;
  }),

  switchFilter: wxapp.handler(function* switchFilter(next, e) {
    const activeFilter = e.currentTarget.dataset.filter;
    const pageData = this.data;
    let sortStatus = pageData.sortStatus;
    if(activeFilter === '筛选'){
      this.actionShow();
      sortStatus = '';
      return;
    } else if (activeFilter === '价格') {
      if(sortStatus === ''){
        sortStatus = 'up';
      } else if(sortStatus === 'up'){
        sortStatus = 'down';
      } else if(sortStatus === 'down'){
        sortStatus = 'up';
      }
    } else{
      sortStatus = '';
    }
    this.setData({
      sortStatus,
      filterActive: activeFilter,
    });

    yield next;
  }),

  // action弹出
  actionShow(){
    const pageData = this.data;
    const animation = wx.createAnimation({
      duration: 200,
    });
    this.animation = animation;

    if (pageData.actionActive === true) {
      animation.right('-642rpx').step();
      this.setData({
        actionActive: false,
      });
    } else {
      animation.right('0').step();
      this.setData({
        actionActive: true,
      });
    }

    this.setData({
      animationData: animation.export(),
    });
  },

  closeAction: wxapp.handler(function* closeAction(next){
    this.animation.right('-642rpx').step();

    this.setData({
      animationData: this.animation.export(),
      actionActive: false,
    });

    yield next;
  }),

  checkboxChange: wxapp.handler(function* checkboxChange(next, e){
    const pageData = this.data;
    let filterDetail = pageData.filterDetail;
    if(filterDetail.isOnly){
      filterDetail.isOnly = false;
    } else{
      filterDetail.isOnly = true;
    }
    this.setData({
      filterDetail,
    });

    yield next;
  }),

  checkType: wxapp.handler(function* checkType(next, e){
    let activeType = e.target.dataset.value;
    if(this.data.activeType === activeType){
      activeType = '';
    }
    this.setData({
      activeType,
    })

    yield next;
  }),

  onShow: wxapp.handler(function* onShow(next) {

    yield next;
  }),

  onLoad: wxapp.handler(function* onLoad(next) {
    const systemInfo = wx.getSystemInfoSync();
    const windowHeight = systemInfo.windowHeight;
    this.setData({
      windowHeight,
    });


    // 加载历史搜索
    var that = this;  
    // 如果缓存中有值，先从缓存中读取  
    var info = wx.getStorageSync(searchHistoryKey); 
    if (info) {
      that.setData({
        historySearchList: info
      });
    } else {
      that.setData({
        historySearchList: []
      });
    }

    var getHotWord = Api.getHotWord();
    // 加载热门搜索
    wx.request({
      url: getHotWord,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {
        
      },
      complete: function (res) {
        that.setData({
          hotSearchList: res.data.data
        });
      }
    });

    var getGoodsSearchType = Api.getGoodsSearchType();
     console.log(getGoodsSearchType);
  var class_id = 0;
    // 商品列表
    wx.request({
      url: getGoodsSearchType,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        id: class_id
      },
      complete: function (res) {
        console.log(111);
        console.log(res);
        that.setData({ filterDetail: res.data.data });
        console.log(that.data);
      }
    });


    yield next;
  }),

  searchSubmit: wxapp.handler(function* searchSubmit(next,e) {
    var that = this;
    var searchValue = e.detail.value;
    var isAdd = true;
    var searchLogData = that.data.historySearchList;

    for (var i = 0; i < searchLogData.length;i++){
      if (searchLogData[i]['name'] == searchValue) {
        isAdd = false;
      }
    }
    // 生成搜索历史
    if ("" != e.detail.value && isAdd){
      searchLogData.push({ name: e.detail.value, url:"../SearchList/SearchList" });
      wx.setStorageSync(searchHistoryKey, searchLogData);  
    }

    // 跳转搜索
    var getGoodsList = Api.getGoodsList();
    if ("" != e.detail.value){
      // 商品列表
      wx.request({
        url: getGoodsList,
        header: {
          'content-type': 'application/json'
        },
        method: 'GET',
        data: {//发送给后台的数据
          title: searchValue
        },
        complete: function (res) {
          that.setData({
            searchValue,
            brandDetail: res.data.data,
            goSearch: true,
          });
        }
      });
    }

    yield next;
  }),

});
